package com.zyl.pagergallery_kt.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zyl.pagergallery_kt.R
import com.zyl.pagergallery_kt.pojo.PhotoItem
import kotlinx.android.synthetic.main.cell_photo.view.*

class PhotoAdapter : ListAdapter<PhotoItem, PhotoViewMolder>(DiffCallBack) {

    object DiffCallBack : DiffUtil.ItemCallback<PhotoItem>() {
        override fun areItemsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem === newItem
        }

        override fun areContentsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem.photoId == newItem.photoId
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewMolder {
        val view: View = LayoutInflater.from(parent.context).inflate(R.layout.cell_photo, parent, false)
        return PhotoViewMolder(view)
    }

    override fun onBindViewHolder(holder: PhotoViewMolder, position: Int) {
        Glide.with(holder.itemView)
                .load(getItem(position).fullUrl)
                .placeholder(R.drawable.ic_baseline_photo_while_24)
                .into(holder.itemView.imageViewPhoto)
    }

}

class PhotoViewMolder(itemView: View) : RecyclerView.ViewHolder(itemView)